package cn.fly.springbucks;

import cn.fly.springbucks.beans.MyBean1;
import cn.fly.springbucks.data.mybatis.mapper.CoffeeMapper;
import cn.fly.springbucks.data.mybatis.model.Coffee;
import cn.fly.springbucks.service.CoffeeService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@EnableTransactionManagement
@SpringBootApplication
@EnableRedisRepositories
@MapperScan("cn.fly.springbucks.data.mybatis.mapper")
public class SpringbucksApplication implements ApplicationRunner {

    public static void main(String[] args) {
        SpringApplication.run(SpringbucksApplication.class, args);
    }

    @Autowired
    private CoffeeService coffeeService;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private MyBean1 myBean1;

    @Override
    public void run(ApplicationArguments args) throws Exception {

//		generateArtifacts();
//      playWithArtifacts();
        AutowiredTest();
    }

    private void AutowiredTest() throws Exception {
        log.info("自动装配："+myBean1.get());
        log.info("返回json");
        log.info("获取所有coffee：localhost:8080/coffee/?pageNum=1");
        log.info("获取所有coffee：localhost:8080/coffee/?name=espresso");
        log.info("新增coffee：   localhost:8080/coffee/addcoffee?name=espresso2&price=CNY 30");
        log.info("返回xml1,设置请求Accept：application/xml");
        log.info("获取所有coffee：localhost:8080/coffee/?pageNum=1");
        log.info("获取所有coffee：localhost:8080/coffee/?name=espresso");
        log.info("新增coffee：   localhost:8080/coffee/addcoffee?name=espresso2&price=CNY 30");
        log.info("返回xml2");
        log.info("获取所有coffee：localhost:8080/coffee/xml/?pageNum=1");
        log.info("获取所有coffee：localhost:8080/coffee/xml/?name=espresso");
        log.info("新增coffee：   localhost:8080/coffee/xml/addcoffee?name=espresso2&price=CNY 30");
    }

    private void generateArtifacts() throws Exception {
        List<String> warnings = new ArrayList<>();
        ConfigurationParser cp = new ConfigurationParser(warnings);
        Configuration config = cp.parseConfiguration(
                this.getClass().getResourceAsStream("/generatorConfig.xml"));
        DefaultShellCallback callback = new DefaultShellCallback(true);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }
    private void playWithArtifacts() throws Exception {
        log.info(dataSource.toString());
        System.out.println("====分页查询第一种=====");
        System.out.println("=========================分页查询第1页===========================");
        coffeeService.getAllCoffeeMenu(1,0).forEach(c -> System.out.println(c.toString()));
//        System.out.println(new PageInfo(coffeeService.getAllCoffeeMenu(1,0)));
        System.out.println("=========================分页查询第2页===========================");
        coffeeService.getAllCoffeeMenu(2,0).forEach(c -> System.out.println(c.toString()));
        System.out.println("=========================分页查询第0页===========================");
        coffeeService.getAllCoffeeMenu(0,0).forEach(c -> System.out.println(c.toString()));
        System.out.println("====分页查询第二种=====");
        System.out.println("=========================分页查询第1页===========================");
        coffeeService.getAllCoffeeMenu(1,1).forEach(c -> System.out.println(c.toString()));
        System.out.println("=========================分页查询第2页===========================");
        coffeeService.getAllCoffeeMenu(2,1).forEach(c -> System.out.println(c.toString()));
        System.out.println("=========================分页查询第0页===========================");
        coffeeService.getAllCoffeeMenu(0,1).forEach(c -> System.out.println(c.toString()));

        System.out.println();
        for (int i = 0;i<3;i++)
            System.out.println(coffeeService.getCoffeeByName("espresso").toString());

        for (int i = 0;i<3;i++)
            System.out.println(coffeeService.getCoffeeByName("latte").toString());

        Thread.sleep(5000);
        for (int i = 0;i<3;i++)
            System.out.println(coffeeService.getCoffeeByName("espresso").toString());

        System.out.println();
        System.out.println("=========================事务更新===========================");
        System.out.println("=========================更新前查询===========================");
        coffeeService.getAllCoffeeMenu(0,0).forEach(c -> System.out.println(c.toString()));
        System.out.println("=========================更新价格===========================");
        System.out.println("=========================更新第四条是抛出异常===========================");
        try {
            coffeeService.updatePriceByIDTran(getCoffees(5000));
        }catch (Exception e){
            System.out.println("=========================事务更新失败回滚===========================");
        }
        System.out.println("=========================更新后查询===========================");
        coffeeService.getAllCoffeeMenu(0,0).forEach(c -> System.out.println(c.toString()));

        System.out.println();

        System.out.println("=========================非事务更新===========================");
        System.out.println("=========================更新前查询===========================");
        coffeeService.getAllCoffeeMenu(0,0).forEach(c -> System.out.println(c.toString()));
        System.out.println("=========================更新价格===========================");
        System.out.println("=========================更新第四条是抛出异常===========================");
        try {
            coffeeService.updatePriceByID(getCoffees(8000));
        }catch (Exception e){
            System.out.println("=========================非事务更新不回滚===========================");
        }
        System.out.println("=========================更新后查询===========================");
        coffeeService.getAllCoffeeMenu(0,0).forEach(c -> System.out.println(c.toString()));

    }

    public List<Coffee> getCoffees(long price){
        ArrayList<Coffee> coffees = new ArrayList<>();
        coffees.add(Coffee.builder().name("espresso").price(Money.ofMinor(CurrencyUnit.of("CNY"), price)).build());
        coffees.add(Coffee.builder().name("latte").price(Money.ofMinor(CurrencyUnit.of("CNY"), price)).build());
        coffees.add(Coffee.builder().name("capuccino").price(Money.ofMinor(CurrencyUnit.of("CNY"), price)).build());
        coffees.add(Coffee.builder().name("mocha").price(Money.ofMinor(CurrencyUnit.of("CNY"), price)).build());
        coffees.add(Coffee.builder().name("macchiato").price(Money.ofMinor(CurrencyUnit.of("CNY"), price)).build());
        return coffees;
    }
}
